package com.jiuzhe.commercial.ui.house;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.ImageView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.help.Tip;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.HouseStayBean;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huanghao on 2018/6/2.
 */

public class HomeStayMapLocationActivity extends BaseActivity{

    private Tip tip;
    AMap mAmap;
    @BindView(R.id.map)
    MapView mMap;
    @BindView(R.id.tv_location)
    EditText tvLocation;
    @BindView(R.id.iv_center_location)
    ImageView mIvCenterLocation;
    @BindView(R.id.iv_location)
    ImageView mIvLocation;
    private LatLonPoint point;
    private String name;
    private String roomNum,regionId;
    private float zoom = 17;//地图缩放级别
    private ObjectAnimator mTransAnimator;//地图中心标志动态
    private LatLng latLng;
    private  HouseStayBean houseStayBean =  new HouseStayBean();
    private String city;
    private String area;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMap.onCreate(savedInstanceState);
        //初始化地图控制器对象
        if (mAmap == null) {
            mAmap = mMap.getMap();
        }
        UiSettings mUiSettings = mAmap.getUiSettings();
        mUiSettings.setZoomControlsEnabled(false);//是否显示地图中放大缩小按钮
        mTransAnimator = ObjectAnimator.ofFloat(mIvCenterLocation, "translationY", 0f, -80f, 0f);
        mTransAnimator.setDuration(1600);
        mTransAnimator.setInterpolator(new LinearInterpolator());//插值器
       // mTransAnimator.setRepeatCount(-1);//-1 代表无限循环执行
        initListener();
        getMapLoation();
    }




    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_homestay_map_location);
        setTitle("房源地图显示");
        setRringTitle("下一步");
        tip = getIntent().getParcelableExtra("tip");
        roomNum = getIntent().getStringExtra("roomNum");
        city = getIntent().getStringExtra("city");
        area = getIntent().getStringExtra("area");
        point = tip.getPoint();
        name = tip.getAddress();
        String location = tip.getDistrict()+ name;
        tvLocation.setText(location);
        tvLocation.setSelection(location.length());
        setOnClik();
    }

    private void setOnClik() {
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                houseStayBean.setAddress(tip.getDistrict() + name);
                houseStayBean.setRoomNo(roomNum);
                houseStayBean.setCity(city);
                houseStayBean.setArea(area);
                Intent intent = new Intent(HomeStayMapLocationActivity.this,HomeStayTypeActivity.class);
                intent.putExtra("room",(Serializable)houseStayBean);
                startActivity(intent);
            }
        });
    }



    @Override
    protected void loadData() {


    }

    private void initListener() {
        //监测地图画面的移动
        mAmap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
                if (null != cameraPosition ) {
                    mAmap.clear();
                    houseStayBean.setLat((int)((cameraPosition.target.latitude)*1000000));
                    houseStayBean.setLng((int)((cameraPosition.target.longitude)*1000000));
                    startTransAnimator();
                }
            }

            @Override
            public void onCameraChange(CameraPosition cameraPosition) {

            }
        });
    }

    private void moveCenter(LatLng latLng) {
        if (null != mAmap) {
            mAmap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
        }
    }

    private void getMapLoation() {
        MarkerOptions markerOptions = new MarkerOptions();
       // markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_market)); //图标
       // markerOptions.icon(BitmapDescriptorFactory.fromView(getView()));
        markerOptions.position(new LatLng(point.getLatitude(), point.getLongitude())); //位置
     //   markerOptions.period(60); //设置多少帧刷新一次图片资源
        mAmap.addMarker(markerOptions);
        mAmap.clear();
        moveCenter(new LatLng(point.getLatitude(), point.getLongitude()));
    }

    private View getView() {
        View view = this.getLayoutInflater().inflate(R.layout.map_location_item_view, null);
        return view;
    }


    /**
     * 移动动画
     */
    private void startTransAnimator() {
        if (null != mTransAnimator && !mTransAnimator.isRunning()) {
            mTransAnimator.start();
        }
    }

    @OnClick({R.id.iv_location})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.iv_location:
                mIvLocation.setImageResource(R.mipmap.location_gps_green);
                moveCenter(new LatLng(point.getLatitude(), point.getLongitude()));
                houseStayBean.setLat((int)(point.getLatitude()*1000000));
                houseStayBean.setLng((int)(point.getLongitude()*1000000));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMap != null)
        mMap.onDestroy();
    }
}
